﻿using Appliaction.Dto.Base_Manage;
using Appliaction.IAppServices.Base_Manage;
using AutoMapper;
using Domain.Entitys.Base_Manage;
using Domain.IRepositorys.Base_Manage;
using EFCore.Repositorys.Base_Manage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appliaction.AppServices.Base_Manage
{
    public class UserService : IUserService
    {
        private readonly IUserRepository userRepository;
        private readonly IMapper mapper;

        public UserService(IUserRepository userRepository,IMapper mapper)
        {
            this.userRepository = userRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 根据用户名和部门分页查询用户和角色信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dept_id"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<List<Sys_User>> GetSys_UsersAsync(string name, int dept_id, int currentPage, int pageSize)
        {
            return await userRepository.GetSys_UsersAsync(name,dept_id,currentPage,pageSize);
        }

        /// <summary>
        /// 根据用户名和部门查询出用户总数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dept_id"></param>
        /// <returns></returns>
        public async Task<int> GetSys_UsersCountAsync(string name, int dept_id)
        {
            return await userRepository.GetSys_UsersCountAsync(name, dept_id);
        }

        /// <summary>
        /// 查询全部用户
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<List<Sys_User>> GetUsersAsync(int dept, string name)
        {
            return userRepository.GetUsersAsync(dept,name);
        }

        public Task<Sys_User> LoginAsync(Sys_User user)
        {
            return userRepository.LoginAsync(user);
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="userAddDto"></param>
        /// <returns></returns>
        public async Task<int> Sys_UserAddAsync(UserAddDto userAddDto)
        {
            Sys_User user = mapper.Map<Sys_User>(userAddDto);
            return await userRepository.Sys_UserAddAsync(user);
        }

        /// <summary>
        /// 逻辑删除用户信息
        /// </summary>
        /// <param name="userDeleteDto"></param>
        /// <returns></returns>
        public async Task<int> Sys_UserDeleteAsync(UserDeleteDto userDeleteDto)
        {
            Sys_User sys_User = mapper.Map<Sys_User>(userDeleteDto);
            return await userRepository.Sys_UserDeleteAsync(sys_User);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userUpdateDto"></param>
        /// <returns></returns>
        public async Task<int> Sys_UserUpdateAsync(UserUpdateDto userUpdateDto)
        {
            Sys_User sys_User = mapper.Map<Sys_User>(userUpdateDto);
            return await userRepository.Sys_UserUpdateAsync(sys_User);
        }
    }
}
